﻿using System;
using System.Collections.Generic; // gerir notkun á sniðmátum möguleg
using System.Linq;
using System.Web;

/*
* University of Reykjavík
* Brjóstagjöf.is is the product of first-year practical programming course
* for computer science students at said university
*
* URL: http://code.google.com/p/scags 
* Authors: 
* Ástvaldur Sigurðsson
* Guðjón Jónsson
* Kristín Inga Vigfúsdóttir
* Steinar Bragi Sigurðsson
* Sveinn Ernstsson
*/
namespace skags.Models
{
    /*
    * Samskipti við töflur Links og LinksCategory í gagnagrunni
    */
    public class LinksRepository
    {
        // tilvik af gagnagrunni (vísun/tenging í gagnagrunn) búin til
        DBDataContext dc = new DBDataContext();

        // Vistar breytingar
        public void Save()
        {
            dc.SubmitChanges();
        }

        /* 
         * Skilar öllum tenglum.
         * Notað af LinkController.
         */
        public IEnumerable<LinksViewModel> GetAll()
        {
            var result = (from s in dc.LinkCategories
                                 orderby s.Title ascending
                                 select new LinksViewModel
                                            {
                                                Category = s,
                                                Links = (from l in dc.Links
                                                         where l.CategoryID == s.ID
                                                         select l)
                                            });


            return result;
        }

        /*
         * Sækir alla flokka og linka.
         * Notað af LinkController.
         */
        public LinksTablesViewModel GetLinksByCategoryID(int id)
        {
            var allCategories = from s in dc.LinkCategories
                                orderby s.Title ascending
                                select s;

            var allLinks = from s in dc.Links
                              where s.CategoryID == id
                              orderby s.ID descending
                              select s;

            return new LinksTablesViewModel { Links = allLinks, Categories = allCategories };
        }

        /* 
         * Sækir alla tengla
         * Notað af AdminController.
         */ 
        public IEnumerable<LinksItemViewModel> GetAllLinks()
        {
            var result = from a in dc.Links
                         join c in dc.LinkCategories on a.CategoryID equals c.ID
                         join u in dc.aspnet_Users on a.CreatorID equals u.UserId
                         orderby a.ID descending
                         select new LinksItemViewModel
                         {
                             ID = a.ID,
                             CreatorID = a.CreatorID,
                             CreatorName = u.UserName,
                             Title = a.Title,
                             Url = a.Url,
                             CategoryID = c.ID,
                             CategoryName = c.Title
                         };

            return result;
        }

        /*
         * Skilar tengli eftir númeri
         */ 
        public Link GetLinkByID(int id)
        {
            return dc.Links.SingleOrDefault(u => u.ID == id);
        }

        /*
         * Skilar flokki tengils eftir númeri
         * Notað í AdminController.
         */ 
        public LinkCategory GetLinkCategoryByID(int id)
        {
            return (from s in dc.LinkCategories
                    where s.ID == id
                    select s).SingleOrDefault();
        }

        /*
         * Skilar öllum flokkum.
         * Notað í AdminController
         */ 
        public IEnumerable<LinkCategory> GetAllCategories()
        {
            var result = from s in dc.LinkCategories
                         orderby s.ID descending
                         select s;

            return result;
        }

        /*
         * Naer í tengil eftir númeri.
         * Notað af AdminController.
         */ 
        public LinkCatViewModel GetLinkCatByID(int id)
        {
            var theLink = dc.Links.SingleOrDefault(u => u.ID == id);
            var allCategories = GetAllCategories();

            return new LinkCatViewModel
            {
                ID = theLink.ID,
                CreatorID = theLink.CreatorID,
                Title = theLink.Title,
                Url = theLink.Url,
                CategoryID = theLink.CategoryID,
                Categories = allCategories
            };
        }

        /* 
         * Uppfærir tengil.
         * Notað af AdminController.
         */
        public void UpdateLink(Link item)
        {
            var theLink = GetLinkByID(item.ID);
            if (theLink != null)
            {
                theLink.Title = item.Title;
                theLink.Url = item.Url;
                theLink.CategoryID = item.CategoryID;

                Save();
            }
        }

        /* 
         * Uppfærir tenglaflokk. 
         * Notað af AdminController.
         */
        public void UpdateLinkCategory(LinkCategory item)
        {
            var theCat = GetLinkCategoryByID(item.ID);
            if (theCat != null)
            {
                theCat.Title = item.Title;
            }

            Save();
        }

        /* 
         * Býr til tengil.
         * Notað af AdminController.
         */
        public void AddLink(Link item)
        {
            dc.Links.InsertOnSubmit(item);
            Save();
        }

        /* 
         * Býr til flokk.
         * Notað af AdminController.
         */
        public void AddLinkCategory(LinkCategory item)
        {
            dc.LinkCategories.InsertOnSubmit(item);
            Save();
        }

        /* 
         * Eyðir tengli.
         * Notað af AdminController.
         */
        public void DeleteLink(int id)
        {
            var itemToDelete = GetLinkByID(id);

            dc.Links.DeleteOnSubmit(itemToDelete);

            Save();
        }

        /*
         * Eyðir tenglaflokk og öllum hans tenglum
         * Notað af AdminController.
         */ 
        public void DeleteLinkCategory(int id)
        {
            // Finnum allar greinar í þessum flokk
            var linkToDelete = from s in dc.Links
                               where s.CategoryID == id
                               select s;
            // Finnum færsluna í gagnagrunninum
            var catToDelete = GetLinkCategoryByID(id);

            dc.Links.DeleteAllOnSubmit(linkToDelete);
            dc.LinkCategories.DeleteOnSubmit(catToDelete);

            Save();
        }
    }
}